#!/usr/bin/env python
# coding:utf-8

def goods_detail_item(date1,date2):
    '''
    将goods_detail账单的商品购买记录,和item账单相应的物品添加记录 合并在一行
    仅限 UseMeans=1(51点购买)、PayMeans=2(计时)的商品
    '''
    import common.db
    bill_connection = common.db.connect('billDB')
    bill_cursor = bill_connection.cursor()

    #goods_detail
    sql1='''
insert into statisticdb_dmz.goods_detail_item (TransID,ItemID,ZoneID,DestID,
    g_BillTime,g_RoleID,g_AccountID,g_RoleName,g_Gender,g_Experience,g_Type,g_PayMeans,g_Price,g_UseMeans,g_Quantity) 
select TransID,GoodsID,ZoneID,if(DestID>0,DestID,RoleID) DestID,
    BillTime,RoleID,AccountID,RoleName,Gender,Experience,`Type`,PayMeans,Price,UseMeans,Quantity
    from dmz_bill_%s.goods_detail where UseMeans=1 and PayMeans=2
on duplicate key update g_info=g_info+1
    '''
    #item
    sql2='''
insert into statisticdb_dmz.goods_detail_item (TransID,ItemID,ZoneID,DestID,
    i_BillTime,i_ResultID,i_UseMeans,i_QuantityOld,i_AddReq,i_AddReal,i_QuantityNew)
select TransID,ItemID,ZoneID,RoleID,
    BillTime,ResultID,UseMeans,QuantityOld,AddReq,AddReal,QuantityNew
    from dmz_bill_%s.item where UseMeans=1
     and (TransID like 'Z01TP10ID%%' or TransID like 'Z02TP10ID%%' or TransID like 'Z101TP10ID%%')
     and exists( 
         select 1 from statisticdb_dmz.goods_detail_item
         where TransID=item.TransID and ItemID=item.ItemID and ZoneID=item.ZoneID and DestID=item.RoleID and g_UseMeans=1 and g_PayMeans=2
     )
on duplicate key update i_BillTime=values(i_billTime),i_ResultID=values(i_ResultID),i_UseMeans=values(i_UseMeans),
    i_QuantityOld=values(i_QuantityOld),i_AddReq=values(i_AddReq),i_AddReal=values(i_AddReal),i_QuantityNew=values(i_QuantityNew),i_info=i_info+1
    '''
    import logging
    from datetime import timedelta
    one_day=timedelta(days=1)

    date=date1
    while date<=date2:
        date_number=date.strftime('%Y%m%d')

        num1=bill_cursor.execute(sql1 %(date_number))

        num2=bill_cursor.execute(sql2 %(date_number))
        bill_connection.commit()

        date_line=date.strftime('%Y-%m-%d')
        logging.log(logging.INFO,'%s : %d  %d=2*%d'%(date_line,num1,num2,num2/2))

        date+=one_day
    bill_cursor.close()
    bill_connection.close()

def arg_exit(tip=''):
    usage='usage:\n\tpython %s %s  [Y-m-d | Y-m-d~Y-m-d]' % (sys.argv[0],sys.argv[1])
    sys.exit(tip+'\n'+usage)

if __name__ != "__main__":
    import sys
    if len(sys.argv)!=3:
        arg_exit('argument count error')
    arg=sys.argv[2]
    count=arg.count('~')

    from datetime import datetime
    import time
    try:
        if count==0:
            date1=date2=datetime(*time.strptime(arg,'%Y-%m-%d')[0:3])
        elif count==1:
            date1,date2=arg.split('~')
            date1=datetime(*time.strptime(date1,'%Y-%m-%d')[0:3])
            date2=datetime(*time.strptime(date2,'%Y-%m-%d')[0:3])
        else:
            raise ValueError('should contain at most one "~"')
    except ValueError,msg:
        arg_exit('invalid argument value "'+arg+'":\n\t'+str(msg))

    goods_detail_item(date1,date2)


